package com.crishne.anwo.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crishne.anwo.dao.UserMapDao;
import com.crishne.anwo.model.UserMap;
import com.crishne.anwo.service.UserMapService;
import com.crishne.anwo.utill.PaginationUtil;

@Service
@Transactional
public class UserMapServiceImpl implements UserMapService {

	 @Autowired
	 private UserMapDao userMapDAO;
	 
	@Override
	public void addUserMap(UserMap userMap) {
		userMapDAO.addUserMap(userMap);

	}

	@Override
	public void updateUserMap(UserMap userMap) {
		userMapDAO.updateUserMap(userMap);

	}

	@Override
	public void deleteUserMap(String stUserMapId) {
		userMapDAO.deleteUserMap(stUserMapId);

	}
	
	@Override
	public void deleteUserMapByUserId(String stUserId) {
		userMapDAO.deleteUserMapByUserId(stUserId);
	}

	@Override
	public UserMap selectUserMapById(String userNapId) {
		return userMapDAO.selectUserMapById(userNapId);
	}

	@Override
	public List<UserMap> getUserMapRoles(String userId) {
		return userMapDAO.getUserMapRoles(userId);
	}

	@Override
	public List<UserMap> getRoleMapUsers(String roleId) {
		return userMapDAO.getRoleMapUsers(roleId);
	}

	@Override
	public PaginationUtil<UserMap> searchUserMaps(
			PaginationUtil<UserMap> pageConditions) {
		return (PaginationUtil<UserMap>) userMapDAO.searchUserMaps(pageConditions);
	}
	
	@Override
	public void saveRoleMaps(String userId,String[] roleIds){
		//先删除所有所属角色
		userMapDAO.deleteUserMapByUserId(userId);
		
		//添加选择角色
		for(int i=0;i<roleIds.length;i++){
			if(!"".equals(roleIds[i].trim())){
				UserMap um = new UserMap();
				um.setStUserMapId(UUID.randomUUID().toString());
				um.setStUserId(userId);
				um.setStRoleId(roleIds[i]);
				userMapDAO.addUserMap(um);
			}
		}
	}

}
